package Controller;

import Util.Constants;
import Util.Page;
import com.sun.corba.se.spi.activation.LocatorPackage.ServerLocationPerORB;
import entity.Category;
import entity.Product;
import service.CategoryService;
import service.impl.CategoryServiceimpl;
import service.impl.productServiceimpl;
import service.productService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/*
 *徐韬
 *2021/10/9
 *10:56
 */
@WebServlet("/product")
public class ProductController extends BaseController {
    public String showAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String pageIndex = request.getParameter("pageIndex");
        if(pageIndex==null){
            pageIndex="1";
        }
        Page page = new Page(Integer.valueOf(pageIndex));
        productService productService = new productServiceimpl();
        List<Product> productList = productService.showAll(page);
        productList.forEach(System.out::println);
        request.setAttribute("page", page);
        request.setAttribute("productList",productList);


        return Constants.FORWARD+"/list.jsp";
    }
    public String showAllCat(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        CategoryService categoryService = new CategoryServiceimpl();
        List<Category> categoryList = categoryService.showAll();
        request.setAttribute("categoryList",categoryList);
        return Constants.FORWARD+"/edit.jsp";

    }
    public String modfiy(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        String pName = request.getParameter("pName");
        Integer cId = Integer.valueOf(request.getParameter("cId"));
        String pDateStr = request.getParameter("pDate");
        String pVender = request.getParameter("pVender");
        String pPrice = request.getParameter("pPrice");
        Integer pType = Integer.valueOf(request.getParameter("pType"));
        System.out.println(id);
        System.out.println(pName);

        Date date = null;
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-mm-dd");
            date = simpleDateFormat.parse(pDateStr );
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Product product = new Product(0,pName,date,pVender,Double.valueOf(pPrice),pType,null);
        System.out.println(product);
            if(id==""){
                 productService productService = new productServiceimpl();
                 productService.addproduct(product,Integer.valueOf(cId));
                 return Constants.REDIRECT+"/product?method=showAll";

            }else {
            product.setpId(Integer.valueOf(id));
            productService productService = new productServiceimpl();
            productService.update(product,Integer.valueOf(cId));
            return Constants.REDIRECT+"/product?method=showAll";

            }

    }
    public String select(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer id = Integer.valueOf(request.getParameter("id"));
        productService productService = new productServiceimpl();
        Product product = productService.show(id);
        request.setAttribute("product", product);
        CategoryService categoryService = new CategoryServiceimpl();
        List<Category> categoryList = categoryService.showAll();
        request.setAttribute("categoryList",categoryList);
        return Constants.FORWARD+"/edit.jsp";


    }
    public String delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer id = Integer.valueOf(request.getParameter("id"));
        productService productService = new productServiceimpl();
        productService.delete(id);
        return Constants.REDIRECT+"/product?method=showAll";



    }


}
